﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Collections;
using System.Collections.Generic;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/roadwaydetail")]
    public class E_RoadWaydetailController : STHPMS1.Web.W_Core.MyController
    {

        [Route("")]
        [Route("page/{page?}", Name = "GetRoadwaydetailByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string inspecid = null, string road = null, string admdiv = null, string admgrd = null, string rd = null, string pvmttype = null, string manaut = null, int page = 1, int rows = 10)
        {
            /*
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<E_Roadwaydetail, bool>> exp = null;
            if (road != null && road.ToString().Length > 0)
                exp = pre => pre.Rdwayid == road.ToString();
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Rdwayid == road.ToString() && pre.Inspecid == inspecid.ToString();
            if (inspecid != null && inspecid.ToString().Length > 0 && d != null && d.ToString().Length > 0)
                exp = pre => pre.Rdwayid == road.ToString() && pre.Inspecid == inspecid.ToString() && pre.Direction_Dictid==Convert.ToInt16(d);
            var data = this.Scene.Bll.E_Roadwaydetail.Get<E_Roadwaydetail>(exp, info).ToArray();
            if (data == null) return NotFound();
            
            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
             */

            string search = "";
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            if (inspecid != null && inspecid.ToString().Length > 0)
            {
                search += " and INSPECID='" + inspecid + "'";
            }
            if (road == null)
            {
                List<string> list= this.Scene.Bll.E_Roadwaydetail.GetRdwayID(inspecid);
                if(list.Count>0)
                {
                    string strvalues = string.Empty;
                    foreach (string str in list)
                    {
                        strvalues += str + ",";
                    }
                    search += " and B_INVNTORY.RDWAYID in ('" + strvalues.Substring(0, strvalues.Length - 1) + "')";
                }
                
            }

            if (road != null && road.ToString().Length > 0)
            {
                search += " and B_INVNTORY.RDWAYID='" + road + "'";
            }
            if (admdiv != null && admdiv.ToString().Length > 0)
            {
                //search += " and B_INVNTORY.ADMDIV='430000'";
                if (admdiv.Substring(2) == "0000") //省级
                {
                    //info.Admdiv.Substring(0,2)
                    search += " and B_INVNTORY.ADMDIV like '%" + admdiv.Substring(0, 2) + "%' ";
                }
                else if (admdiv.Substring(4) == "00") //市级
                {
                    //info.Admdiv.Substring(0,4)
                    search += " and B_INVNTORY.ADMDIV like '%" + admdiv.Substring(0, 4) + "%' ";
                }
                else  //区域
                {
                    search += " and B_INVNTORY.ADMDIV='" + admdiv + "'";
                }
            }
            if (admgrd != null && admgrd.ToString().Length > 0)
            {
                search += " and B_INVNTORY.ADMGRD =" + admgrd + "";
            }
            if (rd != null && rd.ToString().Length > 0)
            {
                search += " and B_INVNTORY.RDCLASS_DICTID =" + rd + "";
            }
            if (pvmttype != null && pvmttype.ToString().Length > 0)
            {
                search += " and B_INVNTORY.PVMTTYPE_DICTID =" + pvmttype + "";
            }
            if (manaut != null && manaut.ToString().Length > 0)
            {
                search += " and B_INVNTORY.MANAUT='" + manaut + "'";
            }

            var data = this.Scene.Bll.E_Roadwaydetail.Get<E_Roadwaydetail>(search,info).ToArray();

            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }


        //[Route("inspecid")]
        //public dynamic GetInspecid(string rdwayid)
        //{
        //    //var innerJoinQuery =
        //    //    from road in this.Scene.Bll.E_Roadwaydetail.GetInspecid(rdwayid)
        //    //    select new { id = road.Value, text = road.Value };
        //    //return innerJoinQuery;


        //    //return Ok(this.Scene.Bll.E_Roadwaydetail.GetInspecid(rdwayid));
        //    var innerJoinQuery =
        //        from road in this.Scene.Bll.E_Roadwaydetail.GetInspecid(rdwayid)
        //        select new { id = road, text = road };
        //    return innerJoinQuery;

        //}

        [Route("getinspecid")]
        public dynamic GetInspecid()
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.E_Roadwaydetail.GetInspecid()
                select new { id = road, text = road };
            return innerJoinQuery;

        }

        [Route("getroadwayid")]
        public dynamic GetRoadwayID(string inspecid)
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.E_Roadwaydetail.GetRdwayID(inspecid)
                select new { id = road, text = road };
            return innerJoinQuery;
        }

        //[Route("evalusave")]
        //public void GetEvaluSave(string inspecid,string rdwayid,int d)
        //{

        //    this.Scene.Bll.E_Evalu.Insert(inspecid, rdwayid, d,"");
        //}


        
    }
}